<?php
/**
 * country.php
 *
 * Created on 2014-12-5
 * Create by LDD
 */
namespace haibao\cms\data;

use \haibao\cms\model\filter\Country as CountryFilter;

class Country extends \haibao\cms\data\BaseMysql{
	
	private static $xcacheCountryDictName = 'AllCountry';
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\Country');
	}
	/**
	 * 获取所有国家
	 */
	public function getAllCountry(){
		$cacheCountryDict = $this->getCache(self::$xcacheCountryDictName);
		return $cacheCountryDict ? json_decode($cacheCountryDict) : $this->setAllCountry();
	}
	/**
	 * 
	 */
	public function setAllCountry(){
		$CountryFilter = new CountryFilter();
		$CountryFilter->select(array(
				CountryFilter::CONDITION_FIELD_NAME_COUNTRY_NAME,
				CountryFilter::CONDITION_FIELD_NAME_DATA_VALUE
		));
		$allCountry = parent::getAll($CountryFilter);
		$this->setCache(self::$xcacheCountryDictName,json_encode($allCountry));
		return $allCountry;
	}
	
	public function getCountryByCountryValue($countryValue){
		$country = json_decode($this->getCache($countryValue));
		if(!$country){
			$CountryFilter = new CountryFilter();
			$CountryFilter->where(CountryFilter::CONDITION_FIELD_NAME_DATA_VALUE, \haibao\frame\data\query\Condition::CONDITION_EQUAL, $countryValue);
			$country = parent::getOne($CountryFilter);
			$this->setCache($countryValue, json_encode($country));
		}
		return $country;
	}
}
